/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.web.jsf.beans.admin.groups;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.ajax4jsf.model.SequenceRange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.GroupDao;
import ar.com.greensoft.neo.model.Group;
import ar.com.greensoft.neo.search.SearchInput;
import ar.com.greensoft.neo.search.TextSearchInput;
import ar.com.greensoft.neo.util.SelectEntity;
import ar.com.greensoft.neo.web.jsf.beans.common.AbstractApplicationMenu;
import ar.com.greensoft.neo.web.jsf.beans.common.GenericEditRevisionFacadeBean;
import ar.com.greensoft.neo.web.jsf.beans.common.NavegationConstants;
import ar.com.greensoft.neo.web.jsf.util.JSFMenuUtil;

/**
 * @author Jorge E. Villaverde
 * @version 1.0.0
 * @since 08/12/2011	
 */
@Component
@RequestScoped
@ManagedBean
public class GroupBean extends AbstractApplicationMenu implements NavegationConstants, Serializable {

	private static final String ENTITY_PREFIX = "groups";
	private static final String MENU_NAME = "menuAdminGroups";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private GroupDao groupDao;
	
	/**
	 * Group Data Model
	 */
	private GroupDataModel dataModel;

	private SearchInput input = new TextSearchInput(); 

	public ExtendedDataModel<SelectEntity<Group>> getDataModel() {
		if(this.dataModel == null)
			this.dataModel = new GroupDataModel();
		return dataModel;
	}
	
	public SearchInput getInput() {
		if(input == null)
			this.input = new TextSearchInput(); 
		return input;
	}
	
	public void search() {
		if(input != null)
			input.setSearchActive(true);
		if(this.dataModel != null)
			this.dataModel.reset();
	}
	
	public boolean isSearchActive() {
		if(input == null)
			return false;
		return input.isSearchActive();
	}

	public String showMenu() {
		return JSFMenuUtil.buildDefaultMenuPath(ADMIN_PATH, ENTITY_PREFIX);
	}	
	
	@Override
	public String getMenuName() {
		return MENU_NAME;
	}
	
	private class GroupDataModel extends ExtendedDataModel<SelectEntity<Group>> {
		private String rowKey = null;
		private int rowCount = -1;
		private List<SelectEntity<Group>> cache = null;
		
		public GroupDataModel() {
			super();
			reset();
		}
		
		@Override
		public Object getRowKey() {
			return rowKey;
		}

		@Override
		public void setRowKey(Object rowKey) {
			this.rowKey = (String)rowKey;
		}

		@Override
		public void walk(FacesContext fc, DataVisitor dv, Range range, Object o) {
	        SequenceRange sr = (SequenceRange) range;
	        int currentRow = sr.getFirstRow();
	        int rows = sr.getRows();
	        
			try {
				if(input != null){
					this.rowCount = -1;
					this.cache = SelectEntity.asSelectedEntityList(groupDao.findEntriesBySearchInput(input, currentRow, rows));
			        for (SelectEntity<Group> t : cache) {
			        	dv.process(fc, t.getEntity().getName(), o);
			        }
				}
			} catch (DaoException e) {
			}
		}

		@Override
		public int getRowCount() {
			if(rowCount == -1){
				try {
					//rowCount = (int)getDao().count();
					
					if(input != null){
						rowCount = (int)groupDao.countBySearchInput(input);
					}
				} catch (DaoException e) {
					GenericEditRevisionFacadeBean.addErrorMessage(e.getMessage());
				}
			}
			return rowCount;
		}

		@Override
		public SelectEntity<Group> getRowData() {
			for(SelectEntity<Group> t : cache){
				if(t.getEntity().getName().equals(rowKey))
					return t;
			}
			return null;
		}

		@Override
		public int getRowIndex() {
			return -1;
		}

		@Override
		public Object getWrappedData() {
			throw new UnsupportedOperationException();
		}

		@Override
		public boolean isRowAvailable() {
			return rowKey != null;
		}

		@Override
		public void setRowIndex(int arg0) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void setWrappedData(Object arg0) {
			throw new UnsupportedOperationException();
		}

		public void reset() {
			this.rowKey = null;
			this.rowCount = -1;
			this.cache = null;
		}	
	}
	
	public void exportToExcel(){
		
	}
}